package com.bookstore.servlet;

import com.bookstore.pojo.Book;
import com.bookstore.services.BookService;
import com.bookstore.services.Impl.BookServiceImpl;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "BookServlet", value = "/BookServlet")
public class BookServlet extends BaseServlet {
    BookService bookService=new BookServiceImpl();

    /**
     * 根据分页查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNo = request.getParameter("pageNo");
        String size = request.getParameter("size");
        System.out.println(pageNo+""+size);
        PrintWriter writer = response.getWriter();
        Gson gson=new Gson();
        List<Book> pageByPrice = bookService.getPageByPrice( pageNo, size);
        PageInfo<Book> pageInfo=new PageInfo<>(pageByPrice);
        String toJson = gson.toJson(pageInfo);
        System.out.println(toJson);
        writer.write(toJson);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String index = request.getParameter("id");
        int id = 0;
        try {
            id=Integer.parseInt(index);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        bookService.delete(new Book(id,null,null,null,null,null,null));
    }

    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String readLine = request.getReader().readLine();
        Gson gson=new Gson();
        Book book1 = gson.fromJson(readLine, Book.class);
        Book one = bookService.getOne(book1);
        PrintWriter writer = response.getWriter();
        writer.write(one.toString());
    }

    protected void updateBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String parameter = reader.readLine();
        System.out.println("parameter:"+parameter);
        Gson gson=new Gson();
        Book book = gson.fromJson(parameter, Book.class);
        System.out.println("update:"+book);
        boolean update = bookService.update(book);
        System.out.println(update);
    }

    protected void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String readLine = request.getReader().readLine();
        Gson gson=new Gson();
        Book book = gson.fromJson(readLine, Book.class);
        boolean add = bookService.add(book);
    }
}
